/*
 * Copyright (c) 2017 5utrip.com. All Rights Reserved.
 */
package com.guoyu.project.service;

import com.google.common.collect.Maps;
import com.guoyu.project.common.constant.Constants;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.SHA256Utils;
import com.guoyu.project.dao.UserInfoDao;
import com.guoyu.project.model.UserInfo;
import com.guoyu.project.model.enums.AccountType;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户信息,业务类UserInfoService
 *
 * @author: xblibo Thu Jun 22 10:23:25 CST 2017
 * @version 1.0.0
 */
@Service
public class UserInfoService {

    @Resource
    private UserInfoDao userInfoDao;

    /**
     * 保存用户信息数据
     *
     * @param userInfo
     */
    public void saveUserInfo(UserInfo userInfo) {
        userInfoDao.save(userInfo);
    }

    /**
     * 更新用户信息实体
     *
     * @param userInfo
     */
    public void updateUserInfo(UserInfo userInfo) {
        userInfoDao.update(userInfo);
    }

    /**
     * 删除用户信息数据
     *
     * @param userInfoId
     */
    public void deleteById(int userInfoId) {
        userInfoDao.deleteById(userInfoId);
    }

    /**
     * 根据id获取用户信息数据
     *
     * @param userInfoId
     * @return
     */
    public UserInfo queryById(int userInfoId) {
        return userInfoDao.queryById(userInfoId);
    }

    /**
     * 根据条件分页查询用户信息数据
     *
     * @param offset
     * @param limit
     * @param params
     * @return
     */
    public Pagination<UserInfo> queryUserInfos(int offset, int limit, Map<String, Object> params) {
        // 根据条件获取总记录数
        int total = userInfoDao.queryUserInfoCount(params);

        if (null == params) {
            params = Maps.newHashMap();
        }
        params.put(Constants.START_POSITION, offset);
        params.put(Constants.PAGE_SIZE, limit);

        // 分页查询数据
        List<UserInfo> userInfoList = userInfoDao.queryUserInfos(params);

        return new Pagination(offset, limit, total, userInfoList);
    }

    public UserInfo querySingleUser(Map<String, Object> params) {
        List<UserInfo> userInfoList = userInfoDao.queryUserInfos(params);
        if (CollectionUtils.isEmpty(userInfoList)) {
            return null;
        }
        return userInfoList.get(0);
    }

    public UserInfo queryByName(String userName) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userName", userName);
        return querySingleUser(params);
    }

    public UserInfo queryByPhone(String phone) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("phone", phone);
        return querySingleUser(params);
    }

    public UserInfo queryByEmail(String email) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("email", email);
        return querySingleUser(params);
    }

    
    /**
     * 注册
     * @param userName
     * @param phone
     * @param email
     * @param password
     * @param accountType
     */
    public void register(String userName, String phone, String email, String password, AccountType accountType) {
        if (StringUtils.isBlank(userName)) {
            throw new RuntimeException("用户名不能为空");
        }
        if(userName.equals("root") || userName.equals("admin") || userName.equals("root".toUpperCase())
                || userName.equals("admin".toUpperCase())){
            throw new RuntimeException("用户名不合格");
        }
        if (StringUtils.isBlank(password)) {
            throw new RuntimeException("用户密码不能为空");
        }
        if (StringUtils.isBlank(phone)) {
            throw new RuntimeException("用户电话不能为空");
        }
        if (StringUtils.isBlank(email)) {
            throw new RuntimeException("用户邮箱不能为空");
        }

        UserInfo userInfo = queryByName(userName);
        if (userInfo != null) {
            throw new RuntimeException("用户名已存在");
        }
        userInfo = queryByPhone(phone);
        if (userInfo != null) {
            throw new RuntimeException("用户电话已存在");
        }
        userInfo = queryByEmail(email);
        if (userInfo != null) {
            throw new RuntimeException("用户邮箱已存在");
        }
        userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setPassword(SHA256Utils.getSHA256String(password));
        userInfo.setPhone(phone);
        userInfo.setEmail(email);
        userInfo.setAccountType(accountType);
        saveUserInfo(userInfo);
    }

    public void modifyPassword(int userId, String password) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setPassword(DigestUtils.md5Hex(password));
        updateUserInfo(userInfo);
    }

}